
const Fly = require("flyio/dist/npm/wx")
const flyhttp = new Fly() 
import { baseUrl } from './config';

flyhttp.config.baseURL = baseUrl;
/* @desc:不拦截的请求名 */
const unInterceptorUrl = [ ]

//不弹loading的请求名
const unLoadingUrl = [ ]

/* @desc:检测请求名是否需要拦截 */
function checkUrl(resp) {
    let arr = resp.split('?')[0].split('/'), flag = true
    unInterceptorUrl.forEach(item => {
        if (item === arr[arr.length - 2] + '/' + arr[arr.length - 1]) {
            flag = false
        }
    })
    return flag
}

/* @desc:检测不弹loading的请求名*/
function checkLoadUrl(resp) {
    let arr = resp.url.split('/'), flag = true
    unLoadingUrl.forEach(item => {
        if (item === arr[arr.length - 2] + '/' + arr[arr.length - 1]) {
            flag = false
        }
    })
    return flag
}

flyhttp.interceptors.request.use((request) => { 
    if (checkLoadUrl(request)) {
        // wx.showLoading({ title: '拼命加载中...' })
    }
    return request
}) 
flyhttp.interceptors.response.use(function (response) {   
    wx.hideLoading();
    if (response.data.code != 300000 && checkUrl(response.engine.responseURL)) {
        console.log(response.data.err);
        wx.showToast({
            title: response.data.err,
            icon: 'none'
        })
        return response
    }else{ 
        return response
    }
})
export default flyhttp